package com.example.service.wechat.impl;

import com.example.mapper.web.BikeMapper;
import com.example.mapper.web.StopMapper;
import com.example.pojo.web.Bike;
import com.example.pojo.web.Result;
import com.example.pojo.web.Stop;
import com.example.pojo.wechat.*;
import com.example.service.wechat.ShapeService;
import com.example.utils.ResultUtil;
import org.geotools.geojson.geom.GeometryJSON;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.Polygon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @ClassName ShapeServiceImpl
 * @Description: com.example.service.wechat.impl
 * @Auther: xiwd
 * @Date: 2022/4/16 - 04 - 16 - 15:37
 * @version: 1.0
 */
@Service
public class ShapeServiceImpl implements ShapeService {
    //properties
    @Autowired
    private StopMapper stopMapper;
    @Autowired
    private BikeMapper bikeMapper;

    //methods
    @Override
    public Result getAllWxPolygon_StopPlaques() {
        try{
            List<Stop> stops = stopMapper.selectStopsByName_Belong(null, null);
            List<WxPolygon> wxPolygons = new ArrayList<>();
            /**
             * AtomicReference-An object reference that may be updated atomically.
             */
            AtomicReference<WxPolygon> wxPolygon = new AtomicReference<>();
            //遍历
            stops.stream().forEach(e->{
                //获取Polygon对象
                Polygon polygon = e.getGeom();
                //获取坐标数组
                Coordinate[] coordinates = polygon.getCoordinates();
                //创建WxPolygon对象
                wxPolygon.set(new WxPolygon());
                //边框颜色
                wxPolygon.get().setStrokeColor(WxColor.RED.getColorHexCode());
                //填充颜色
                wxPolygon.get().setFillColor(WxColor.LIGHTGRAY.getColorHexCode());
                //边框宽度
                wxPolygon.get().setStrokeWidth(1);
                //获取坐标值
                List<WxPoint> wxPoints = new ArrayList<>();
                WxPoint wxPoint = null;
                //添加坐标对
                for (int i = 0; i < coordinates.length; i++) {
                    wxPoint = new WxPoint(coordinates[i].getY(),coordinates[i].getX());
                    wxPoints.add(wxPoint);
                }
                //设置坐标值
                wxPolygon.get().setPoints(wxPoints);
                //添加WxPolygon到列表中
                wxPolygons.add(wxPolygon.get());
            });
            return ResultUtil.success(1,"查询成功",wxPolygons);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(0,"查询失败");
        }
    }

    @Override
    public Result getAllWxMarker_Bikes() {
        try {
            GeometryJSON geometryJSON = new GeometryJSON();
            List<Bike> bikeList = bikeMapper.selectAllBikes();
            //数据结果
            List<WxMarker> wxMarkers = new ArrayList<>();
            //解析数据
            bikeList.forEach(e->{
                String location = e.getLocation();
                try {
                    WxLabel wxLabel = new WxLabel();
                    Geometry geometry = geometryJSON.read(new StringReader(location));
                    double y = geometry.getCoordinate().getY();//纬度
                    double x = geometry.getCoordinate().getX();//经度
                    WxMarker wxMarker = new WxMarker();
                    wxMarker.setId(e.getId());
                    wxMarker.setClusterId(e.getId());
                    wxMarker.setJoinCluster(true);
                    wxMarker.setLatitude(y);
                    wxMarker.setLongitude(x);
                    wxMarker.setTitle(e.getBno());
                    wxMarker.setWidth(16);
                    wxMarker.setHeight(24);
//                    wxMarker.setIconPath("../../icons/marker.png");
                    //设置Label
                    wxLabel.setContent(e.getBikeStatus().getDescription());
                    wxLabel.setColor(WxColor.LABELRED.getColorHexCode());
                    wxLabel.setTextAlign("right");
                    wxMarker.setLabel(wxLabel);
                    //添加WxMarker到List列表中
                    wxMarkers.add(wxMarker);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            });
            return ResultUtil.success(1,"查询成功",wxMarkers);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(0,"查询失败");
        }
    }


}
